package com.fis.vaadin.validator;

import sun.security.action.GetBooleanAction;

import com.vaadin.data.validator.AbstractValidator;
import com.vaadin.ui.AbstractField;
import com.vaadin.ui.Field;

public class CompareIntegerValidate extends AbstractValidator {

	private long integerTarget = 0;
	private long integerTargetBefore = 0;
	private long integerTargetAfter = 0;
	private Field fieldTarget = null;
	private int conditional;
	public static final String CASE_NORMAL = "NORMAL";
	public static final String CASE_BETWEEN = "BETWEEN";
	private String useCase = CASE_NORMAL;

	public CompareIntegerValidate(String errorMessage) {
		super(errorMessage);
		setUseCase(CASE_NORMAL);
	}

	public CompareIntegerValidate(String errorMessage,
			long integerTargetBefore, long integerTargetAfter, String useCase) {
		super(errorMessage);
		setUseCase(useCase);
		setIntegerTargetAfter(integerTargetAfter);
		setIntegerTargetBefore(integerTargetBefore);
	}

	public CompareIntegerValidate(String errorMessage, long integerTarget,
			int conditional) {
		super(errorMessage);
		setIntegerTarget(integerTarget);
		this.conditional = conditional;
	}

	public CompareIntegerValidate(String errorMessage, Field fieldTarget,
			int conditional) {
		super(errorMessage);
		setFieldTarget(fieldTarget);
		this.conditional = conditional;
	}

	@Override
	public boolean isValid(Object value) {
		String strValue = value.toString().trim();
		if (strValue.length() < 1)
			return true;

		long val;
		try {
			val = Long.parseLong(strValue);
		} catch (NumberFormatException e) {
			return false;
		}
		
		if (getUseCase().equals(CASE_BETWEEN)) {
			return ((getIntegerTargetBefore() <= val) && (val <= getIntegerTargetAfter()));
		} else if (getUseCase().equals(CASE_NORMAL)) {
			
			if (getFieldTarget() != null)
			{
				setIntegerTarget(Long.parseLong(getFieldTarget().getValue().toString()));
			}
			
			if (conditional == -1)
				return val < getIntegerTarget();
			if (conditional == 0)
				return val == getIntegerTarget();
			if (conditional == 1)
				return val > getIntegerTarget();
			if (conditional == 2) {
				return (val > getIntegerTarget() || val == getIntegerTarget());
			}
			if (conditional == -2) {
				return (val < getIntegerTarget() || val == getIntegerTarget());
			}
			return false;
		}
		return false;
	}

	public long getIntegerTarget() {
		return integerTarget;
	}

	public void setIntegerTarget(long integerTarget) {
		this.integerTarget = integerTarget;
	}

	public long getIntegerTargetBefore() {
		return integerTargetBefore;
	}

	public void setIntegerTargetBefore(long integerTargetBefore) {
		this.integerTargetBefore = integerTargetBefore;
	}

	public long getIntegerTargetAfter() {
		return integerTargetAfter;
	}

	public void setIntegerTargetAfter(long integerTargetAfter) {
		this.integerTargetAfter = integerTargetAfter;
	}

	public String getUseCase() {
		return useCase;
	}

	public void setUseCase(String useCase) {
		this.useCase = useCase;
	}

	public Field getFieldTarget() {
		return fieldTarget;
	}

	public void setFieldTarget(Field fieldTarget) {
		this.fieldTarget = fieldTarget;
	}

}
